package edu.hawaii.teams.halealohacli.command;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import org.wattdepot.client.WattDepotClient;

/**
 * Tests the EnergySince class.
 * 
 * @author Branden Ogata
 * 
 */
public class TestEnergySince {

  /**
   * Tests the getData and getDate methods in the EnergySince class. Note that any test dates must
   * come after the server reset on 23 November 2011 (technically 22 November 2011, but the first
   * full day of data is 23 November).
   * 
   * @throws Exception Handles any errors that do occur, such as timeouts.
   * 
   */
  @Test
  public void testGetData() throws Exception {
    // Create data to test against
    WattDepotClient client = new WattDepotClient("http://server.wattdepot.org:8190/wattdepot/");
    EnergySince sample = new EnergySince();
    // Constant double used to convert from watt to kilowatt.
    final double KILO = 1000.0;
    final String testSource = "Ilima";
    final String testStartDate = "2011-11-23";

    // Test the getData method
    // Test with valid input
    assertTrue("Testing getData with valid input",
        Double.valueOf(sample.getData(client, testSource, testStartDate)) / KILO > 0);

    // Test with multiple input
    assertFalse("Testing getData with multiple inputs",
        sample.getData(client, testSource, testStartDate, "Lehua", testStartDate)
            .contains("Error:"));
    // Test with invalid client
    assertEquals("Testing getData with invalid client",
        sample.getData(new WattDepotClient("client/"), testSource, testStartDate),
        "Error: An error occurred while retrieving data.\n\n");

    // Test with invalid source
    assertEquals("Testing getData with invalid source",
        sample.getData(client, "Beta", testStartDate),
        "Error: The source Beta could not be found.\n\n");

    // Test with invalid date
    assertEquals("Testing getData with invalid date",
        sample.getData(client, testSource, "yyyy-mm-dd"),
        "Error: An error occurred while parsing the date.\n\n");

    // Test with invalid date formatting
    assertEquals("Testing getData with invalid date formatting",
        sample.getData(client, testSource, "Beta"),
        "Error: An error occurred while parsing the date; " + "the formatting is incorrect.\n\n");

    // Test with date that is out of bounds
    assertEquals("Testing getData with out-of-bounds date",
        sample.getData(client, testSource, "2011-34-64"),
        "Error: An error occurred in using the XMLGregorianCalendar class.\n\n");

    // Test with date before server reset
    assertEquals("Testing getData with old date", sample.getData(client, testSource, "2011-02-25"),
        "Error: An error occurred in the XML from the server.\n\n");
    
    // Test with date before server reset
    assertEquals("Testing getData with old date",
                  sample.getData(client, testSource, "2011-02-25"),
                  "Error: An error occurred in the XML from the server.\n\n");
  }
}